<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 {
            width: 1000px;
            margin: 0 auto;
            height: 1000px;
            background-color: pink;
        }

        .box2 {
            width: 1000px;
            margin: 0 auto;
            height: 1000px;
            background-color: rgb(213, 235, 165);
        }

        .box3 {
            width: 1000px;
            margin: 0 auto;
            height: 1000px;
            background-color: rgb(192, 229, 255);
        }
    </style>
</head>

<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
</body>
<!-- 
在滚动事件中，实现一个节流函数，使得滚动事件的处理函数不会过于频繁地被调用。
 -->
<script>
    // 获取滚动条与顶部的距离
    // window.scrollY
    window.addEventListener('scroll', throttle(function () {
        console.log('滚动条滚动');
    }, 2000));
    function throttle(fn, time) {
        var start = Date.now();
        return function () {
            var end = Date.now();
            if (end - start > time) {
                fn();
                start = Date.now();
            }
        }

    }


</script>

</html>